/*
 * Copyright 2015 Pivotal Software, Inc..
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.joomtu.smarterlife.utils;

/**
 *
 * @author huguobing
 */
public class HelpUtil {

    private static HelpUtil instance;

    private static final String ATOM = "[^\\x00-\\x1F^\\(^\\)^\\<^\\>^\\@^\\,^\\;^\\:^\\\\^\\\"^\\.^\\[^\\]^\\s]";

    private static final String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*";

    private static final String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]";

    private final java.util.regex.Pattern emailPattern = java.util.regex.Pattern.compile(
            "^" + ATOM + "+(\\." + ATOM + "+)*@"
            + DOMAIN
            + "|"
            + IP_DOMAIN
            + ")$",
            java.util.regex.Pattern.CASE_INSENSITIVE
    );

    private final java.util.regex.Pattern mobilePattern = java.util.regex.Pattern.compile(
            "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"
    );

    public static HelpUtil getInstance() {
        if (instance == null) {
            instance = new HelpUtil();
        }
        return instance;
    }

    public Boolean isEmail(String value) {
        if (value == null || value.length() == 0) {
            return true;
        }
        return emailPattern.matcher(value).matches();
    }

    public Boolean isMobile(String value) {
        if (value == null || value.length() == 0) {
            return true;
        }
        return mobilePattern.matcher(value).matches();
    }

    public static Boolean isEmpty(String value) {
        return "".equals(value) || value == null;
    }
}
